const app = getApp()

import {
    resetPwd,
    sendMsg
} from '../../../api/user';
import { hexMD5 } from "../../../utils/md5.js"
import {getInfo} from "../../../api/member.js";

Page({
    data: {
        isShow:true,
        showPassword: true,
        disabled: false,
        time: 0,
        verifyCode:"",
        newPwd:"",
        confirmPwd:"",
        username:'',
        mobile:'',
        sendMsg: {},
        resetPwd: {},
        getInfo:{}
    },
    onShow() {
        this.getInfo()
    },
    getInfo(){
        getInfo().then(res=>{
            this.data.mobile = res.data.member.mobile
            this.setData({
                mobile: this.data.mobile
            })
        })
    },
    getConfirmPwd(e){
        this.data.confirmPwd = e.detail.value
        this.setData({
            confirmPwd: this.data.confirmPwd
        })
    },
    getNewPwd(e){
        this.data.newPwd = e.detail.value
        this.setData({
            newPwd: this.data.newPwd
        })
    },
    getVerifyCode(e){
        this.data.verifyCode = e.detail.value
        this.setData({
            verifyCode: this.data.verifyCode
        })
    },
    getMobile(e){
        this.data.mobile = e.detail.value
        this.setData({
            mobile: this.data.mobile
        })
    },
    getGetInfo() {
        getInfo().then(res => {
            this.data.mobile = res.data.member.mobile
        })
    },
    getSendMsg() {
        sendMsg({
            tplKey: 'register',
            username: this.data.mobile
        }).then(res => {
            if (res.type == "success") {
                wx.showToast({
                    icon: "none",
                    title: '验证码已发送',
                    duration: 5000
                })
            }else{
                wx.showToast({
                    icon: "none",
                    title: '验证码发送失败，请稍后重试！',
                    duration: 5000
                })
            }
        })
    },
    sendcode() {
        if(this.data.mobile == ""){
            wx.showToast({
                title: '请输入手机号码',
                icon: 'none',
                duration: 2000
              })
              return false
        }
        if (this.data.time == 0) {
            this.getSendMsg()
        }
        this.data.time = 30;
        this.setData({
            time: this.data.time
        })
        this.timer();
        this.data.isShow = false
        this.setData({
            isShow:this.data.isShow
        })

    },
    timer() {
        if (this.data.time > 0) {
            this.data.disabled = true;
            this.data.time--;
            this.setData({
                disabled:this.data.disabled,
                time:this.data.time
            })
            setTimeout(()=>{
                this.timer()
            }, 1000);

        } else {
            this.data.time = 0;
            this.data.disabled = false;
            this.data.isShow = true
            this.setData({
                time:this.data.time,
                disabled:this.data.disabled,
                isShow:this.data.isShow
            })
        }
    },
    confirmAlter() {
        if(!this.data.newPwd.length){
            wx.showToast({
                icon: "none",
                title: '密码不能为空',
                duration: 5000
            })

            return
        }
        if(this.data.newPwd !== this.data.confirmPwd){
            wx.showToast({
                icon: "none",
                title: '密码不一致',
                duration: 5000
            })
            return
        }
        let params ={
            mchId:  app.globalData.mchId,
            password: hexMD5(this.data.confirmPwd),
            // username: this.data.mobile,
            username: 'mch_'+ app.globalData.mchId +'_'+ this.data.mobile.trim(),
            verifyCode: this.data.verifyCode
        }
        console.log(params ,"paramsparamsparams")

        resetPwd({},params).then(res => {
            if (res.type == "success") {
                wx.showToast({
                    icon: "none",
                    title: '密码修改成功！',
                    duration: 5000
                })
                wx.navigateBack({
                    delta: 1
                })
            }else{
                wx.showToast({
                    icon: "none",
                    title: '密码修改失败，请稍后重试！',
                    duration: 5000
                })
            }
        })
    }
})
